package br.gov.ufrpe.siglog.controller;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import br.gov.ufrpe.siglog.model.Logradouro;

public class LogradouroDAO implements ILogradouroDAO {

	private Session session;

	public void setSession(Session session) {
		this.session = session;
	}

	@Override
	public void salvar(Logradouro logradouro) {
		this.session.save(logradouro);
	}

	@Override
	public void atualizar(Logradouro logradouro) {
		this.session.update(logradouro);
	}

	@Override
	public void excluir(Logradouro logradouro) {
		this.session.delete(logradouro);
	}

	@Override
	public Logradouro buscarPorCodigo(Integer codigo) {
		return (Logradouro) this.session.get(Logradouro.class, codigo);
	}

	@Override
	public Logradouro buscarPorNome(String nome) {
		String hql = "select u from Logradouro u where u.nome = :nome";
		Query consulta = this.session.createQuery(hql);
		consulta.setString("nome", nome);
		return (Logradouro) consulta.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Logradouro> listar() {
		return this.session.createCriteria(Logradouro.class).list();
	}

}
